package sy.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.model.AppPlant;
import sy.model.IotEntranceURI;
import sy.model.IotParkingURI;
import sy.model.IotVideoURI;
import sy.model.SysOwnerRenterMapping;
import sy.model.SysProject;
import sy.service.IotEntranceURIServiceI;
import sy.service.IotParkingURIServiceI;
import sy.service.IotVideoURIServiceI;
import sy.util.MessageObject;
import sy.util.Tree;

@Controller
@RequestMapping("iotParkingURIController")
public class IotParkingURIController {
	@Autowired
	IotParkingURIServiceI iotParkingURIService;
	
	/************************** view controller ***************************/

	/**
	 * 
		 * @Title      : objectView
		 * @Description: 首页视图
		 * @param      : model
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 
		 * @throws
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
		List<IotParkingURI> parkingList = iotParkingURIService.list();
		
		model.addAttribute("parkingList",parkingList);
		return "iotInterface/parkingURI/parkingURI";
	}
	
	/**
	 * 
		 * @Title      : newObjectInfo
		 * @Description:  TODO
		 * @param      :@param model
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:05:21
		 * @throws
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		IotParkingURI parkingURI = new IotParkingURI();

		model.addAttribute("parkingURI",parkingURI);
		model.addAttribute("typeCode",1);
		
		return "iotInterface/parkingURI/parkingURIFrom";
	}

	/**
	 * 
		 * @Title      : editObjectInfo
		 * @Description:  TODO
		 * @param      :@param id
		 * @param      :@param model
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:06:20
		 * @throws
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam int id,Model model){
		
		IotParkingURI parkingURI = iotParkingURIService.getIotParkingURIById(id);

		model.addAttribute("parkingURI",parkingURI);
		model.addAttribute("typeCode",2);
		
		return "iotInterface/parkingURI/parkingURIFrom";
	}
	
	/************************** data controller ***************************/
	/**
	 * 
	 * @Title		:getIotParkingURI
	 * @Description	:TODO
	 * @param		:@return
	 * @return		:List<IotParkingURI>
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午5:22:01
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST)
	private List<IotParkingURI> getIotParkingURI() {
		List<IotParkingURI> list = iotParkingURIService.list();
		return list;
	}
	/**
	 * 
	 * @Title		:getIotParkingURIByKey
	 * @Description	:TODO
	 * @param		:@param key
	 * @param		:@return
	 * @return		:List<IotParkingURI>
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午5:22:49
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getIotParkingURIByKey", method = RequestMethod.POST)
	private List<IotParkingURI> getIotParkingURIByKey(@RequestParam String key) {
		List<IotParkingURI> list = iotParkingURIService.listIotParkingURIByKey(key);
		return list;
	}
	/**
	 * 
	 * @Title		:getIotParkingURIById
	 * @Description	:TODO
	 * @param		:@param id
	 * @param		:@return
	 * @return		:IotParkingURI
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午5:23:45
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getIotParkingURIById", method = RequestMethod.POST)
	private IotParkingURI getIotParkingURIById(@RequestParam Integer id) {
		IotParkingURI iotParkingURI = iotParkingURIService.getIotParkingURIById(id);
		return iotParkingURI;
	}
	/**
	 * 
	 * @Title		:newObjectInfo
	 * @Description	:TODO
	 * @param		:@param iotParkingURI
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午5:25:04
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	private MessageObject newObjectInfo(@RequestBody IotParkingURI iotParkingURI) {
		MessageObject messageObject = new MessageObject();
		try {
			iotParkingURIService.save(iotParkingURI);
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	/**
	 * 
	 * @Title		:editObjectInfo
	 * @Description	:TODO
	 * @param		:@param iotParkingURI
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午5:42:14
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	private MessageObject editObjectInfo( @RequestBody IotParkingURI iotParkingURI) {
		MessageObject messageObject = new MessageObject();
		try {
			iotParkingURIService.update(iotParkingURI);
			messageObject.setMessage("编辑成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("编辑失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	/**
	 * 
	 * @Title		:delObjectInfo
	 * @Description	:TODO
	 * @param		:@param ids
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午5:42:38
	 * @throws
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") String ids) {
		
		MessageObject messageObject = new MessageObject();
		List<Integer> idArray = new ArrayList<Integer>();
		String[] strs = ids.split(",");
		
		for(String str : strs){
			idArray.add(Integer.parseInt(str));
		}
		try {
			iotParkingURIService.delete(idArray);	
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
}
